package com.gxc.integer;

/**
 * 把一个数字用中文表示出来。数字范围为 [0，99999]。
 * 为了方便输出，使用字母替换相应的中文，万W 千Q 百B 十S 零L。
 * 使用数字取代中文数字注:对于 11 应该表示为 一十一(1S1)，而不是十一(S1)输入描述:
 * 数字 0(包含)到99999(包含)。
 * 输出描述:
 * 用字母替换相应的中文，万W 千Q 百B 十S 零L
 * 示例1:
 * 输入
 * 12001
 * 输出
 * 1W2QL1
 */
public class IntegerToStr {

    public static final String[] numStr = {"零", "一", "二", "三", "四", "五"
                                            ,"六", "七", "八", "九"};

    public static void main(String[] args) {
        int num = 10201;
        System.out.println(intToStr(num));
        System.out.println(intToStr2(num));
    }

    public static String intToStr(int num) {
        if (num == 0) return numStr[0];
        if (num<10) return procesGe(num);
        if (num<100) return procesShi(num, false);
        if (num<1000) return procesBai(num, false);
        if (num<10000) return procesQian(num, false);
        if (num<100000) return processWan(num, false);
        return "";
    }

    public static String processWan(int num, boolean zero) {
        int i = num / 10000;
        int remainder = num % 10000;
        if (i == 0) {
            return (zero?"":"零") + procesQian(remainder, true);
        }
        return procesGe(i) + "万" + procesQian(remainder, false);
    }

    public static String procesQian(int num, boolean zero) {
        int i = num / 1000;
        int remainder = num % 1000;
        if (i == 0) {
            return (zero?"":"零") + procesBai(remainder, true);
        }
        return procesGe(i) + "千" + procesBai(remainder, false);
    }

    public static String procesBai(int num, boolean zero) {
        int i = num / 100;
        int remainder = num % 100;
        if (i == 0) {
            return (zero?"":"零") + procesShi(remainder, true);
        }
        return procesGe(i) + "百" + procesShi(remainder, false);
    }

    public static String procesShi(int num, boolean zero) {
        int i = num / 10;
        int remainder = num % 10;
        if (i == 0) {
            return (zero?"":"零") + procesGe(remainder);
        }
        return procesGe(i) + "十" + procesGe(remainder);
    }

    /**
     * 个位数
     * @param num
     * @return
     */
    public static String procesGe(int num) {
        if (num == 0) return "";
        return numStr[num];
    }

    public static String intToStr2(int num) {
        if (num == 0) return numStr[0];

        return numTo99999999(num);
    }

    public static String numTo9(int num) {
        if (num<=0 || num>9) return "";
        return numStr[num];
    }

    public static String numTo99(int num, boolean isBai) {
        if (num<=0 || num>99) return "";

        int i = num / 10;
        int remainder = num % 10;
        if (i == 0) {
            return (isBai?numStr[0]:"") + numTo9(remainder);
        } else {
            return numTo9(i) + "十" + numTo9(remainder);
        }
    }

    public static String numTo999(int num) {
        if (num<=0 || num>999) return "";

        int i = num / 100;
        int remainder = num % 100;
        if (i == 0) {
            return numTo99(remainder, false);
        } else if (remainder == 0){
            return numTo9(i) + "百";
        } else {
            return numTo9(i) + "百" + numTo99(remainder, true);
        }
    }

    public static String numTo9999(int num) {
        if (num<=0 || num>9999) return "";

        int i = num / 1000;
        int remainder = num % 1000;
        if (i == 0) {
            return numTo999(remainder);
        } else if (remainder == 0){
            return numTo9(i) + "千";
        } else {
            if (remainder< 100) return numTo9(i) + "千零" + numTo99(remainder, false);
            return numTo9(i) + "千" + numTo999(remainder);
        }
    }

    public static String numTo99999999(int num) {
        if (num<=0 || num>99999999) return "";

        int i = num / 10000;
        int remainder = num % 10000;
        if (i == 0) {
            return numTo9999(remainder);
        } else if (remainder == 0){
            return numTo9(i) + "万";
        } else {
            if (remainder<1000) return numTo9999(i) + "万零" + numTo999(remainder);
            return numTo9999(i) + "万" + numTo9999(remainder);
        }
    }


}
